package com.android.lovely.ui.activity

import android.os.Bundle
import android.text.InputType
import android.view.View
import android.view.View.OnClickListener
import android.widget.EditText
import android.widget.ImageView
import androidx.core.view.isVisible
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.lifecycleScope
import com.android.lovely.R
import com.android.lovely.api.service.userService
import com.android.lovely.databinding.ActivityEditPwdBinding
import com.android.lovely.framework.BaseActivity
import com.android.lovely.framework.UIController.showToast
import com.android.lovely.framework.launchWithLoading
import com.kotlin.extensions.view.ViewExt.addStatusBarMarginTop
import com.kotlin.extensions.view.ViewExt.clickable

class EditPwdActivity : BaseActivity<ActivityEditPwdBinding>(), OnClickListener {

    override fun initView(savedInstanceState: Bundle?) {
        mBinding.back.addStatusBarMarginTop()
        mBinding.back.clickable(this)
        mBinding.op.addTextChangedListener {
            checkState()
        }
        mBinding.np.addTextChangedListener {
            checkState()
        }
        mBinding.cp.addTextChangedListener {
            checkState()
            mBinding.cp.isSelected = it?.toString() != mBinding.np.text.toString()
            mBinding.error.isVisible = it?.toString() != mBinding.np.text.toString()
        }
        mBinding.nps.clickable(this)
        mBinding.cps.clickable(this)
        mBinding.submit.clickable {
            changePassword()
        }
    }

    private fun changePassword() {
        lifecycleScope.launchWithLoading {
            val params = mapOf("old_pd" to mBinding.op.text.toString(), "new_pd_1" to mBinding.np.text.toString(), "new_pd_2" to mBinding.cp.text.toString(), "check_same" to 1)
            val result = userService.changePwd(params)
            when (result.code) {
                1 -> {
                    showToast(getString(R.string.cpok))
                    finish()
                }

                -61005 -> {
                    showToast(getString(R.string.old_password_error))
                }
            }
        }
    }

    private fun EditText.changeVisible(show: ImageView) {
        inputType = if (show.isSelected.not()) {
            InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
        } else {
            InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
        }
        show.isSelected = show.isSelected.not()
        setSelection(text?.length ?: 0)
    }

    private fun checkState() {
        val enable = mBinding.op.text.isNullOrBlank().not()
                && mBinding.np.text.isNullOrBlank().not()
                && mBinding.cp.text.isNullOrBlank().not()
                && (mBinding.np.text?.length ?: 0) > 5
                && (mBinding.cp.text?.length ?: 0) > 5
                && mBinding.np.text == mBinding.cp.text
        mBinding.submit.isEnabled = enable
        mBinding.disable.isVisible = enable.not()
    }

    override fun onClick(v: View?) {
        when (v) {
            mBinding.back -> onBackPressedDispatcher.onBackPressed()
            mBinding.nps -> {
                mBinding.np.changeVisible(mBinding.nps)
            }

            mBinding.cps -> {
                mBinding.cp.changeVisible(mBinding.cps)
            }
        }
    }
}